// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: "test4-ht5oe"
})
const db = cloud.database()
const Orderdb = db.collection('OrderData')
const _ = db.command
var $ = db.command.aggregate

// 云函数入口函数
exports.main = async (event, context) => {
  console.log(event.userid)

  if(event.id == 'pendEvalue'){
    const Tuinadata =await Orderdb.aggregate()
    .match({
      UserID: event.userid,
      Isevaluated:false
    })
    .lookup({
      from: 'TuinaData',
      localField: 'OrderID',
      foreignField: 'TuinaID',
      as: 'OrderInfo',
    }).sort({
      OrderData:-1
    }).replaceRoot({
      newRoot: $.mergeObjects([ $.arrayElemAt(['$OrderInfo', 0]), '$$ROOT' ])
    }).match({
      TuinaID: _.exists(true)
    })
    .project({
      OrderInfo:0,
      TuinaDetailIntro:0
    })
    .end()
  
    const Meddata = await Orderdb.aggregate()
    .match({
      UserID: event.userid,
      Isevaluated:false
    })
    .lookup({
      from: 'MedicineData',
      localField: 'OrderID',
      foreignField: 'MedicID',
      as: 'OrderInfo',
    }).sort({
      OrderData:-1
    }).replaceRoot({
      newRoot: $.mergeObjects([ $.arrayElemAt(['$OrderInfo', 0]), '$$ROOT' ])
    }).match({
      MedicID: _.exists(true)
    })
    .project({
      OrderInfo:0,
      MedicDetailIntro:0
    })
    .end()
  
    const Sidedata = await Orderdb.aggregate()
    .match({
      UserID: event.userid,
      Isevaluated:false
    })
    .lookup({
      from: 'SidelineData',
      localField: 'OrderID',
      foreignField: 'SidelineID',
      as: 'OrderInfo',
    }).sort({
      OrderData:-1
    }).replaceRoot({
      newRoot: $.mergeObjects([ $.arrayElemAt(['$OrderInfo', 0]), '$$ROOT' ])
    }).match({
      SidelineID: _.exists(true)
    })
    .project({
      OrderInfo:0,
      SidelineDetailIntro:0
    })
    .end()

    return { 
      Tuinadata,Meddata,Sidedata
         }

  }else if(event.id == 'allOrder'){
    const Tuinadata =await Orderdb.aggregate()
    .match({
      UserID: event.userid,
    })
    .lookup({
      from: 'TuinaData',
      localField: 'OrderID',
      foreignField: 'TuinaID',
      as: 'OrderInfo',
    }).sort({
      Isevaluated:1,
      OrderData:-1
    }).replaceRoot({
      newRoot: $.mergeObjects([ $.arrayElemAt(['$OrderInfo', 0]), '$$ROOT' ])
    }).match({
      TuinaID: _.exists(true)
    })
    .project({
      OrderInfo:0,
      TuinaDetailIntro:0
    })
    .end()
  
    const Meddata = await Orderdb.aggregate()
    .match({
      UserID: event.userid,
    })
    .lookup({
      from: 'MedicineData',
      localField: 'OrderID',
      foreignField: 'MedicID',
      as: 'OrderInfo',
    }).sort({
      Isevaluated:1,
      OrderData:-1,
    }).replaceRoot({
      newRoot: $.mergeObjects([ $.arrayElemAt(['$OrderInfo', 0]), '$$ROOT' ])
    }).match({
      MedicID: _.exists(true)
    })
    .project({
      OrderInfo:0,
      MedicDetailIntro:0
    })
    .end()
  
    const Sidedata = await Orderdb.aggregate()
    .match({
      UserID: event.userid,
    })
    .lookup({
      from: 'SidelineData',
      localField: 'OrderID',
      foreignField: 'SidelineID',
      as: 'OrderInfo',
    }).sort({
      Isevaluated:1,
      OrderData:-1
    }).replaceRoot({
      newRoot: $.mergeObjects([ $.arrayElemAt(['$OrderInfo', 0]), '$$ROOT' ])
    }).match({
      SidelineID: _.exists(true)
    })
    .project({
      OrderInfo:0,
      SidelineDetailIntro:0
    })
    .end()
    return { 
      Tuinadata,Meddata,Sidedata
         }
  }else{
  const Tuinadata =await Orderdb.aggregate()
  .match({
    UserID: event.userid,
    isweihu:false
  })
  .lookup({
    from: 'TuinaData',
    localField: 'OrderID',
    foreignField: 'TuinaID',
    as: 'OrderInfo',
  }).replaceRoot({
    newRoot: $.mergeObjects([ $.arrayElemAt(['$OrderInfo', 0]), '$$ROOT' ])
  }).match({
    TuinaID: _.exists(true)
  })
  .project({
    OrderInfo:0,
    TuinaDetailIntro:0
  })
  .end()

  const Meddata = await Orderdb.aggregate()
  .match({
    UserID: event.userid,
    isweihu:false
  })
  .lookup({
    from: 'MedicineData',
    localField: 'OrderID',
    foreignField: 'MedicID',
    as: 'OrderInfo',
  }).replaceRoot({
    newRoot: $.mergeObjects([ $.arrayElemAt(['$OrderInfo', 0]), '$$ROOT' ])
  }).match({
    MedicID: _.exists(true)
  })
  .project({
    OrderInfo:0,
    MedicDetailIntro:0
  })
  .end()

  const Sidedata = await Orderdb.aggregate()
  .match({
    UserID: event.userid,
    isweihu:false
  })
  .lookup({
    from: 'SidelineData',
    localField: 'OrderID',
    foreignField: 'SidelineID',
    as: 'OrderInfo',
  }).replaceRoot({
    newRoot: $.mergeObjects([ $.arrayElemAt(['$OrderInfo', 0]), '$$ROOT' ])
  }).match({
    SidelineID: _.exists(true)
  })
  .project({
    OrderInfo:0,
    SidelineDetailIntro:0
  })
  .end()
  return { 
    Tuinadata,Meddata,Sidedata
       }
}
}